﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Woodbase.CodeBase.UI;
using Microsoft.Http;
using System.Configuration;
using System.Xml.Linq;

namespace WoodbaseVideoRental.Pages.Movies
{
    public partial class List : WoodbaseBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            using (HttpClient client = new HttpClient(ConfigurationManager.AppSettings["webservicebasepath"]))
            {
                if (Request.QueryString.HasKeys())
                {
                    if (Request.QueryString["avaliable"] == "false")
                    {
                        using (HttpResponseMessage msg = client.Get("movies/rented"))
                        {
                            msg.EnsureStatusIsSuccessful();
                            var doc = msg.Content.ReadAsXElement();
                            doc.Add(new XAttribute("rented", true));
                            xmlContent.DocumentContent = doc.ToString();
                            xmlContent.TransformSource = Server.MapPath("~/Styles/XSLT/MovieDetails.xslt");
                            xmlContent.DataBind();
                        }
                    }
                    else
                    {
                        using (HttpResponseMessage msg = client.Get("movies/avaliable"))
                        {
                            msg.EnsureStatusIsSuccessful();
                            var doc = msg.Content.ReadAsXElement();
                            doc.Add(new XAttribute("rented", false));
                            xmlContent.DocumentContent = doc.ToString();
                            xmlContent.TransformSource = Server.MapPath("~/Styles/XSLT/MovieDetails.xslt");
                            xmlContent.DataBind();
                        }
                    }
                }
                else
                {
                    using (HttpResponseMessage msg = client.Get("movies"))
                    {
                        xmlContent.DocumentContent = msg.Content.ReadAsXElement().ToString();
                        xmlContent.TransformSource = Server.MapPath("~/Styles/XSLT/MovieDetails.xslt");
                        xmlContent.DataBind();
                    }
                }
            }
        }
    }
}